package com.oa.gform.client.ui;

import java.util.Collection;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.Widget;
import com.oa.gform.client.validator.GValidationCallback;
import com.oa.gform.client.validator.GValidator;

public class GFieldHidden<T> extends Widget implements GField<T>, HasValue<T>{
	GBaseFieldImpl<T> baseFieldImpl = new GBaseFieldImpl<T>(this);
	private boolean valueChangeHandlerInitialized;
	
	/***
	 * Costruttore di default
	 */
	public GFieldHidden() {
		super();
		
	}
	
	public void setFieldName(String fieldName) {
		baseFieldImpl.setFieldName(fieldName);
	}

	public String getFieldName() {
		return baseFieldImpl.getFieldName();
	}

	public String[] getFieldGroupNames() {
		return baseFieldImpl.getFieldGroupNames();
	}

	public void setFieldGroupNames(String... fieldGroupNames) {
		baseFieldImpl.setFieldGroupNames(fieldGroupNames);
	}

	public boolean isInGroupName(String fieldGroupName) {
		return baseFieldImpl.isInGroupName(fieldGroupName);
	}
	
	public GFieldLabel getGFieldLabel() {
		return baseFieldImpl.getGFieldLabel();
	}

	public void setGFieldLabel(GFieldLabel gfieldLabel) {
		baseFieldImpl.setGFieldLabel(gfieldLabel);
	}
	
	public void addGValidator(GValidator validator) {
		baseFieldImpl.addGValidator(validator);
	}

	public void addGValidators(Collection<GValidator> collection) {
		baseFieldImpl.addGValidators(collection);
	}
	
	@Override
	public void setInError(boolean inError) {
		baseFieldImpl.setInError(inError);
	}

	@Override
	public void Validate(GValidationCallback gValidationCallback) {
		baseFieldImpl.Validate(gValidationCallback);
	}

	@Override
	public void reset() {
		baseFieldImpl.reset();
		setValue(null);
	}

	@Override
	public void setFocus(boolean focused) {
		//Fake
	}


	public HandlerRegistration addChangeHandler(ChangeHandler handler) {
		return addDomHandler(handler, ChangeEvent.getType());
	}
	
	@Override
	public HandlerRegistration addValueChangeHandler(ValueChangeHandler<T> handler) {
	    // Initialization code
	    if (!valueChangeHandlerInitialized) {
	      valueChangeHandlerInitialized = true;
	      addChangeHandler(new ChangeHandler() {
	        public void onChange(ChangeEvent event) {
	          ValueChangeEvent.fire(GFieldHidden.this, getValue());
	        }
	      });
	    }
		return addHandler(handler, ValueChangeEvent.getType());
	}

	
	@Override
	public T getValue() {
		return getFieldValue();
	}

	@Override
	public void setValue(T value) {
		setValue(value, false);
	}

	@Override
	public void setValue(T value, boolean fireEvents) {
		T oldValue = getValue();
	    setFieldValue(value);
	    if (fireEvents) {
	      ValueChangeEvent.fireIfNotEqual(this, oldValue, value);
	    }
		
	}
	
	public void setFieldValue(T value) {
		baseFieldImpl.setFieldValue(value);
	}

	@Override
	public T getFieldValue() {
		// TODO Auto-generated method stub
		return baseFieldImpl.getFieldValue();
	};

	
}
